package dian.qing.li.demo.security.commons;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class RestResult extends BaseObject implements Serializable {
    private static final long serialVersionUID = -5816304413090422336L;
    private int code;
    private String msg;
    private Object data;

    /**
     * 成功
     *
     * @param data 返回的数据
     * @Author: liqingdian
     */
    public static RestResult succeed(Object data) {
        return new RestResult(0, "success", data);
    }

    /**
     * 成功
     *
     * @Author: liqingdian
     */
    public static RestResult succeed() {
        return succeed(null);
    }

    /**
     * 成功
     *
     * @Author: liqingdian
     */
    public static RestResult succeed(String msg, Object data) {
        return new RestResult(0, msg, data);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @Author: liqingdian
     */
    public static RestResult failed(int code, String msg, Object data) {
        return new RestResult(code, msg, data);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @Author: liqingdian
     */
    public static RestResult failed(String msg, Object data) {
        return failed(1, msg, data);
    }

    /**
     * 失败
     *
     * @param msg 失败消息
     * @Author: liqingdian
     */
    public static RestResult failed(String msg) {
        return failed(msg, null);
    }

    /**
     * 将data对象按存入时类型强转
     *
     * @Author: liqingdian
     */
    public <T> T parseData(Class<T> clazz) {
        if (this.data != null) {
            return (T) this.data;
        }
        return null;
    }

}
